#/usr/bin/python
# -*- encoding=UTF-8 -*-

import os.path
import yaml
import ConfigParser

RNC_TYPE = ('cRNC', 'mcRNC', 'ADA')

RNC_LIST_FILE = "rnclist.yaml"

class RncListConfig():
    def __init__(self, yaml_file=None):
        if yaml_file is None:
            self.yaml_file = RNC_LIST_FILE
        else:
            self.yaml_file = yaml_file

    def get(self, name):
        info = None
        if os.path.isfile(self.yaml_file):
            with open(self.yaml_file, 'r') as outfile:
                for data in yaml.load_all(outfile):
                    if name == data.name:
                        info = data
                        break
        return info

    def get_rnc_list(self, rnc_type):
        rnc_list = []
        if os.path.isfile(self.yaml_file):
            with open(self.yaml_file, 'r') as outfile:
                for data in yaml.load_all(outfile):
                    if rnc_type:
                        if rnc_type == data.type:
                            rnc_list.append(data.name)
                    else:
                        rnc_list.append(data.name)
        rnc_list.sort(key=str.lower)
        return rnc_list

    def remove_rnc(self, name):
        all_yaml = []
        is_need_update = False
        if os.path.isfile(self.yaml_file):
            with open(self.yaml_file, 'r') as outfile:
                for data in yaml.load_all(outfile):
                    if name == data.name:
                        is_need_update = True
                    else:
                        all_yaml.append(data)

        if is_need_update:
            self.__dump_all(all_yaml)

        del all_yaml

    def has_rnc(self, name):
        hasrnc = False
        if os.path.isfile(self.yaml_file):
            with open(self.yaml_file, 'r') as outfile:
                for data in yaml.load_all(outfile):
                    if data.name == name:
                        hasrnc = True
                        break
        return hasrnc

    def save(self, info):
        if not self.__update(info):
            self.__add(info)
        return True

    def __update(self, info):
        all_yaml = []
        updated = False
        if os.path.isfile(self.yaml_file):
            with open(self.yaml_file, 'r') as outfile:
                for data in yaml.load_all(outfile):
                    if data.name == info.name:
                        all_yaml.append(info)
                        updated = True
                    else:
                        all_yaml.append(data)

        if updated:
            self.__dump_all(all_yaml)

        del all_yaml
        return updated

    def __add(self, info):
        return self.__dump(info)

    def load_all(self):
        yaml_all = []
        if os.path.isfile(self.yaml_file):
            with open(self.yaml_file, 'r') as outfile:
                for data in yaml.load_all(outfile):
                    yaml_all.append(data)
        return yaml_all

    def __dump_all(self, yaml_list):
        with open(self.yaml_file, "w") as outfile:
            yaml.dump_all(
                yaml_list, 
                outfile, 
                default_flow_style=False, 
                explicit_start=True
            )

    def __dump(self, info):
        with open(self.yaml_file, 'a+') as outfile:
            yaml.dump(
                info, 
                outfile, 
                default_flow_style=False, 
                explicit_start=True
            )


class DefaultConfig():
    def __init__(self, ini_file=None):
        if ini_file is None:
            self.ini_file = "config.ini"
        else:
            self.ini_file = ini_file
        self.cf = ConfigParser.SafeConfigParser()
        if os.path.isfile(self.ini_file):
            self.cf.read(self.ini_file)
        self.sections = self.cf.defaults()

    def save(self):
        self.cf.write(open(self.ini_file, 'w'))

    def get(self, option):
        if self.sections.has_key(option):
            return self.sections[option]
        else:
            return None

    def delete(self, option):
        if self.sections.has_key(option):
            self.cf.remove_option('DEFAULT', option)
            self.save()

    def set(self, option, value):
        self.sections[option] = value
        self.save()

class LoginInfo():
    """This is a struction for login info."""
    def __init__(self, hostname="", username="", password=""):
        self.hostname = hostname
        self.username = username
        self.password = password

class TestEnvInfo():
    """This is a struction for test environment info."""
    def __init__(self, _type=None, name=None):
        self.rnc = LoginInfo()
        self.tester = LoginInfo()
        self.type = None
        self.name = None
        self.ets = None
        if not _type and name:
            tmp_type = name.split("_")[0]
        else:
            tmp_type = _type
        if tmp_type:
            for var in RNC_TYPE:
                if var == tmp_type:
                    self.type = var
                    self.name = name
                    break

def check_login_info(login_info):
    return cmp(dir(login_info), dir(LoginInfo())) == 0

def check_test_info(test_info):
    return cmp(dir(test_info), dir(TestEnvInfo())) == 0 \
            and check_login_info(test_info.rnc) \
            and check_login_info(test_info.tester)



#if __name__ == "__main__":
    #cfg = YamlConfig()
    #info = cfg.get("cRNC_19A")
    #import instance
    #instance.detail(info)
    #info.rnclib.ip="10.56.16.192"
    #cfg.save(info)
    #info.name="cRNC_18"
    #cfg.save(info)
    #yy = cfg.load_all()
    #instance.detail(yy)
